calcBal = function(x,y) {
	var a = parseFloat($(y + '-charge').value);
	var b = parseFloat($(y + '-insurance-payment').value);
	var c = parseFloat($(y + '-patient-responsibility').value);
	var d = parseFloat($(y + '-patient-payment').value);
	var e = parseFloat($(y + '-balance').value);
	if(x == 'charge') {
		a = a.toFixed(2);
		if(a>0) {
			$(y + '-charge').value = a;
		}
		else {
			$(y + '-charge').value = 0;
		}
		if(b>=0) {} //dummy
		else {
			$(y + '-insurance-payment').value = 0;
		}
		if(c>0) {} //dummy
		else {
			$(y + '-patient-responsibility').value = 0;
		}
		if(d>0) {} //dummy
		else {
			$(y + '-patient-payment').value = 0;
		}
		if(e>0) {} //dummy
		else {
			//a = a.toFixed(2);
			$(y + '-balance').value = a;
		}
	}
	if(x == 'insurance-payment') {
		if(b>0) {
			b = b.toFixed(2);
			$(y + '-insurance-payment').value = b;
		}
		else{
			$(y + '-insurance-payment').value = 0;
			b = parseFloat($(y + '-patient-payment').value);
		}
		if(a>0) {
			c = a-b;
			c = c.toFixed(2);
			$(y + '-patient-responsibility').value = c;
			$(y + '-patient-responsibility').parentNode.addClass('diff');
			e = a-b-d;
			e = e.toFixed(2);
			$(y + '-balance').value = e;
			$(y + '-balance').parentNode.addClass('diff');
		}
	}
	if(x == 'patient-responsibility') {
		if(c>0) {
			c = c.toFixed(2);
			$(y + '-patient-responsibility').value = c;
		}
		else {
			$(y + '-patient-responsibility').value = 0;
		}
	}
	if(x == 'patient-payment') {
		if(d>0) {
			d = d.toFixed(2);
			$(y + '-patient-payment').value = d;
		}
		else{
			$(y + '-patient-payment').value = 0;
			d = parseFloat($(y + '-patient-payment').value);
			d = d.toFixed(2);
		}
		if(a>0 && b>0 && c>0) {
			e = c-d;
			e = e.toFixed(2);
			$(y + '-balance').value = e;
			$(y + '-balance').parentNode.addClass('diff');
		}
		else {
			e = c-d;
			e = e.toFixed(2);
			$(y + '-balance').value = e;
			$(y + '-balance').parentNode.addClass('diff');
		}
	}
	if(x == 'client-balance') {
		if(e>0) {
			e = e.toFixed(2);
			$(y + '-balance').value = e;
		}
		else {
			$(y + '-balance').value = 0;
		}
	}
}

window.addEvent('domready', function(){
	
	// when a date is selected in the vacation cal update DB input with a time
	updateHidden = function() {
		if($('date_of_closure')) {
			var d = $('date_vaca').value;
			$('date_of_closure').value = d + " 12:00";
		}
	};
	new DatePicker('.vacation', {
		pickerClass: 'datepicker_jqui', 
		format: 'Y-m-d', 
		inputOutputFormat: 'Y-m-d', 
		positionOffset: { x: 0, y: 5 }, 
		startDay: 0,
		onSelect:updateHidden
	});
	
	
	if($('admin-archives')) {
		$('pricingTable').getElements('ins input').addEvent('focus', function(e){
			this.parentNode.addClass('edit');
			this.store(this.name,this.value);
		});
		$('pricingTable').getElements('ins input').addEvent('blur', function(e){
			var oldValue = this.retrieve(this.name,this.value);
			var newValue = this.value;
			this.parentNode.removeClass('edit');
			if(oldValue != newValue) {
				this.parentNode.addClass('diff');
				// show the save button
				$('admin_archive_save').setStyle('display','inline-block');
				//get the row number and store off
				var idNo = this.getProperty('id');
				idNo = idNo.toInt();
			}
		});
		$('pricingTable').getElements('select').addEvent('change', function(e){
			this.addClass('edit');
			// show the save button
			$('admin_archive_save').setStyle('display','inline-block');
		});
		$('pricingTable').getElements('ins input').addEvent('change', function(e){
			var idNo = this.getProperty('id');
			idNo = idNo.toInt();
			calcBal(this.className,idNo);
		});
	}
	
	
	// go to URL when drop down is changed, forwards to URL w/ today or tomorrow in it
	if($('admin_print')) {
		$('admin_print').addEvent('change', function(e){
			var selIndex = this.selectedIndex;
			var selValue = this.options[selIndex].value;
			if(selIndex > 0) {
				urlroot = urlroot + '/admin-print/' + selValue;
				window.location = urlroot;
			}
		});
	}
	// go to URL when drop down is changed, forwards to URL w/ day in it
	if($('admin_time-schedule')) {
		$('admin_time-schedule').addEvent('change', function(e){
			var selIndex = this.selectedIndex;
			var selValue = this.options[selIndex].value;
			if(selIndex > 0) {
				urlroot = urlroot + '/admin-time-schedule/' + selValue;
				window.location = urlroot;
			}
		});
	}
	// go to URL when drop down is changed, forwards to URL w/ client's id in it
	if($('admin_menu')) {
		$('admin_edit_client').addEvent('change', function(e){
			var selIndex = this.selectedIndex;
			var selValue = this.options[selIndex].value;
			if(selValue > 0) {
				urlroot = urlroot + '/admin-clients/' + selValue;
				window.location = urlroot;
			}
		});
	}
	if($('admin_archives_client')) {
		$('admin_archives_client').addEvent('change', function(e){
			var selIndexA = $('admin_archives_client').selectedIndex;
			var selValueA = $('admin_archives_client').options[selIndexA].value;
			/*var selIndexB = $('admin_archives_payment_status').selectedIndex;
			var selValueB = $('admin_archives_payment_status').options[selIndexB].value;*/
			//console.log(selIndexB);
			if(selIndexA == 0) {
				urlroot = urlroot + '/admin-archives/';
				window.location = urlroot;
			}
			else if(selIndexA > 1) {
				urlroot = urlroot + '/admin-archives/1/' + selValueA;
				window.location = urlroot;
				
			}
			/*else if(selIndexA == 0 && selIndexB > 1) {
				urlroot = urlroot + '/admin-archives/all/' + selValueB;
				window.location = urlroot;
				
			}
			else if(selIndexA > 1 && selIndexB > 1) {
				urlroot = urlroot + '/admin-archives/' + selValueA + '/' + selValueB;
				window.location = urlroot;
				
			}*/
		});
		
	}
	
	// refresh the day page every 1 min so apt info is fresh
	if($('day')) {
		(function(){ window.location = urlroot; }).delay(60000);
	}
	
	var x = new HtmlTable($('pricingTable'), {
		sortIndex:0,
		sortReverse:true,
		classHeadSort:"table-th-sort",
		classHeadSortRev:"table-th-sort-rev",
		classNoSort:"table-th-nosort",
		sortable: true
	});
	
	
	if($('pw_toggle')) {
		$('pw_toggle').addEvent('click', function(e){
			if (this.get('text') == '(show)') {
				this.set('text','(hide)');
				$('pw_no').setStyle('display','none');
				$('pw_yes').setStyle('display','inline-block');
			}
			else {
				this.set('text','(show)');
				$('pw_yes').setStyle('display','none');
				$('pw_no').setStyle('display','inline-block');
			}
		});
	}
	
	if($$('.appointment-button')) {
		$$('input.appointment-button').addEvent('click', function(e){
			this.setStyle('visibility','hidden');
		});
	}
	
	if($$('.admin-make-apt')) {
		$$('.admin_client_select').addEvent('change', function(e){
			var selIndex = this.selectedIndex;
			var selValue = this.options[selIndex].value;
			var selValReflection = selValue;
			selValReflection = selValReflection.replace("@", "-");
			selValReflection = selValReflection.replace(".", "");
			var selText = this.options[selIndex].text;
			var selId = this.id;
			if(selIndex == 0) {
				$('make-apt_' + selId).removeClass('on');
				$('make-apt-faded_' + selId).addClass('on');
				$('admin-name_' + selId).value = "";
				$('admin-email_' + selId).value = "";
				$('admin-reflection-email_' + selId).value = "";
			}
			else {
				$('make-apt_' + selId).addClass('on');
				$('make-apt-faded_' + selId).removeClass('on');
				$('admin-name_' + selId).value = selText;
				$('admin-email_' + selId).value = selValue;
				$('admin-reflection-email_' + selId).value = selValReflection;
				$('client-email_' + selId).value = selValue;
			}
		});
	}
	
	
});

Number.implement({
	/*
	Property: numberFormat
		Format a number with grouped thousands.
	Arguments:
		decimals, optional - integer, number of decimal percision; default, 2
		dec_point, optional - string, decimal point notation; default, '.'
		thousands_sep, optional - string, grouped thousands notation; default, ','
	Returns:
		a formatted version of number.
	Example:
		>(36432.556).numberFormat()  // returns 36,432.56
		>(36432.556).numberFormat(2, '.', ',')  // returns 36,432.56
	*/

	numberFormat : function(decimals, dec_point, thousands_sep) {
		decimals = Math.abs(decimals) + 1 ? decimals : 2;
		dec_point = dec_point || '.';
		thousands_sep = thousands_sep || ',';
	
		var matches = /(-)?(\d+)(\.\d+)?/.exec((isNaN(this) ? 0 : this) + ''); // returns matches[1] as sign, matches[2] as numbers and matches[3] as decimals
		var remainder = matches[2].length > 3 ? matches[2].length % 3 : 0;
		return (matches[1] ? matches[1] : '') + (remainder ? matches[2].substr(0, remainder) + thousands_sep : '') + matches[2].substr(remainder).replace(/(\d{3})(?=\d)/g, "$1" + thousands_sep) + 
				(decimals ? dec_point + (+matches[3] || 0).toFixed(decimals).substr(2) : '');
	}
});